<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\controller\Base;
use app\facade\Basic;
use think\Request;

/**
 * 部门控制器
 */
class Department extends Base
{

    protected $request;
    protected $service;
    protected $validate;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\Department();
        $this->validate = new \app\admin\validate\Department();
    }

    /**
     * @describe 获取部门列表
     * @author: Bobo
     * @date: 2024-06-05 03:04:33
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $pageIndex  = isset($params['pageIndex'])    ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])     ? intval($params['pageSize']) : 10;
        $pid        = isset($params['pid'])          ? intval($params['pid']) : null;
        $name       = isset($params['name'])         ? trim($params['name']) : null;
        $createTime = isset($params['create_time'])  ? trim($params['create_time']) : null;
        $updateTime = isset($params['update_time'])  ? trim($params['update_time']) : null;


        $where = ['is_del' => 0];
        if ($pid > 0) {
            $where['pid'] = $pid;
        }
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($createTime) {
            $where['create_time'] = $createTime;
        }
        if ($updateTime) {
            $where['update_time'] = $updateTime;
        }

        $res = $this->service->getList($where, $pageIndex, $pageSize);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 获取部门详情
     * @author: Bobo
     * @date: 2024-06-05 03:07:37
     * @param [int] $id 部门ID @required
     * @return Renderable
     */
    public function show()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('show')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $res  = $this->service->getOneById($params['id']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 创建部门
     * @author: Bobo
     * @date: 2024-06-05 03:09:17
     * @param [array] $data 创建部门的数据 @required
     * @return Renderable
     */
    public function create()
    {
        $data = $this->request->params;

        if (!$this->validate->scene('create')->check($data)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $data['create_time'] = date('Y-m-d H:i:s');
        $data['creator'] = $this->request->operator_id;

        $result = $this->service->create($data);
        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 更新部门
     * @describe 更新部门
     * @author: Bobo
     * @date: 2024-06-05 03:11:17
     * @param [int] $id 部门ID @required
     * @param [array] $data 更新部门的数据 @required
     * @return Renderable
     */
    public function update()
    {

        $params = $this->request->params;
        $params['updater'] = $this->request->operator_id;

        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $result = $this->service->update($params);

        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除部门
     * @param int $id 部门ID
     */
    /**
     * @describe 删除部门
     * @author: Bobo
     * @date: 2024-06-05 03:13:56
     * @param [int] $id 部门ID @required
     * @return Renderable
     */
    public function delete()
    {


        $params = $this->request->params;
        $params['operator_id'] = $this->request->operator_id;

        if (!$this->validate->scene('delete')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $ids =  $params['ids'];


        $result = $this->service->batchLogicalDelete($params);




        return Basic::json($result);


    }

    /**
     * @describe 
     * @author: Bobo
     * @date: 2024-08-06 19:07:39
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum()
    {
        $params = $this->request->params;
        $res = $this->service->getFieldEnum($params['field']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2024-09-12 18:01:01
     * @param [type] @required
     * @return Renderable
     */
    public function getTreeSelectOption()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('treeSelect')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $res = $this->service->getTreeSelectOption();
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
